import 'package:flutter/material.dart';
import 'package:wan_android/pages/item_artical.dart';

///发现的二级界面
///http://www.wanandroid.com/article/list/0/json?cid=60
class PageSon extends StatefulWidget {
  final totalData;

  PageSon({this.totalData});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    print(totalData['name']);

    return PageSonState();
  }
}

class PageSonState extends State<PageSon> with AutomaticKeepAliveClientMixin {
  ///初始化tab
  final List<Tab> tabs = [];
  final List<Widget> bodyList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getTitle();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return DefaultTabController(
      length: tabs.length,
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.totalData['name']),
          bottom: TabBar(
            tabs: tabs,
            isScrollable: true,
            //为true 的时候 就可以完整显示tab 文字
            indicatorColor: Colors.red,
            //指示器 颜色
            labelColor: Colors.red,
            labelStyle: TextStyle(fontSize: 18.0),
            unselectedLabelColor: Colors.redAccent,
            unselectedLabelStyle: TextStyle(fontSize: 16.0),
            indicatorSize: TabBarIndicatorSize.tab,
          ),
        ),
        body: TabBarView(
          //这个必须是一个新写的页面
          children: bodyList,
        ),
      ),
    );
  }

  _getTitle() async {
    for (var value in widget.totalData['children']) {
      print(value['id']);
      tabs.add(Tab(
        text: value['name'],
      ));

      int cid = value['id'];
      //    http://www.wanandroid.com/article/list/0/json?cid=7
      bodyList.add(ItemWidget(
        firApi: 'http://www.wanandroid.com/article/list/',
        secApi: '/json?cid=$cid',
        isFavorite: false,
        isSearch: false,
      ));
    }
  }

  // TODO: implement wantKeepAlive
  @override
  bool get wantKeepAlive => true;
}
